<template>
  <div class="address">
    <div class="search">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="">
          <el-input
            v-model="formInline.userId"
            placeholder="请输入用户id"
          ></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-input
            v-model="formInline.name"
            placeholder="请输入收货人名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="exportTable">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" sortable label="地址id" width="180">
        </el-table-column>
        <el-table-column prop="userId" label="用户id" width="180">
        </el-table-column>
        <el-table-column prop="name" label="收货人名称"> </el-table-column>
        <el-table-column prop="tel" label="手机号码"> </el-table-column>
        <el-table-column prop="" label="区域地址">
          <template slot-scope="scope">
            <span>{{
              scope.row.province + scope.row.city + scope.row.county
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="addressDetail" label="详细地址">
        </el-table-column>
        <el-table-column prop="isDefault" label="默认">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.isDefault == true">是</el-tag>
            <el-tag v-if="scope.row.isDefault == false">否</el-tag>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page">
      <el-pagination
        background=""
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNumber"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { downloadXlsx } from "../../units/ex.js";
export default {
  data() {
    return {
      //对话框表单
      dialogForm: {},
      //对话框控制
      dialogVisible: false,
      // 分页
      pageSize: 10,
      pageNumber: 1,
      total: 0,
      //表格数据
      tableData: [],
      //搜索栏
      formInline: {
        name: "",
        userId: "",
      },
    };
  },
  methods: {
    //导出
    exportTable() {
      let list = [];
      for (let i = 0; i < this.tableData.length; i++) {
        let arr = [
          this.tableData[i].id,
          this.tableData[i].userId,
          this.tableData[i].name,
          this.tableData[i].tel,
          this.tableData[i].province +
            this.tableData[i].city +
            this.tableData[i].county,
          this.tableData[i].addressDetail,
          this.tableData[i].isDefault,
   
        ];
        list.push(arr);
      }
      downloadXlsx(list, "整表.xlsx");
    },
    //编辑
    upEdit() {
      this.$store.dispatch("user/upEdit", this.dialogForm).then(() => {
        // console.log(res,'res')
        this.dialogVisible = false;
        this.$notify({
          title: "成功",
          message: "更新用户成功",
          type: "success",
        });
      });
    },
    //开对话框
    openDialog(row, index) {
      this.dialogVisible = true;
      this.dialogForm = row;
    },
    //对话框控制
    handleClose(done) {
      done();
    },
    //分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.getTableData();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNumber = val;
      this.getTableData();
    },
    //初始化表格数据
    getTableData() {
      let data = {
        page: this.pageNumber,
        limit: this.pageSize,
        sort: "add_time",
        order: "desc",
      };
      this.$store
        .dispatch("user/homeAddress_getTableData", data)
        .then((res) => {
          // console.log(res,'res')
          this.tableData = res.data.list;
          this.total = res.data.total;
        })
        .catch(() => {});
    },
    //搜索
    onSearch() {
      // console.log(data, "data");
      let data = {
        page: this.pageNumber,
        limit: this.pageSize,
        name: this.formInline.name,
        userId: this.formInline.userId,
        sort: "add_time",
        order: "desc",
      };
      this.$store
        .dispatch("user/homeAddress_getTableData", data)
        .then((res) => {
          // console.log(res,'res')
          this.tableData = res.data.list;
          this.total = res.data.total;
        })
        .catch(() => {});
    },
  },
  created() {
    this.getTableData();
  },
};
</script>

<style>
</style>